#
import sys
sys.path.insert(0, './qlib')
from typing import Dict
import qlib
from qlib.config import REG_CN
from qlib.data import D
import pandas as pd
from apps.exp.data_source import DataSource

class ExpApp(object):
    def __init__(self):
        self.name = 'apps.exp.exp_app.ExpApp'

    @staticmethod
    def startup(params:Dict = {}) -> None:
        print(f'QLib ExpApp v0.0.1')
        if params['task'] == 1:
            ExpApp.demo(params=params)
    
    @staticmethod
    def demo(params:Dict = {}) -> None:
        # interval=1min 近高频数据
        # ExpApp.download_data(params=params)
        ExpApp.check_data(params=params)

    @staticmethod
    def download_data(params:Dict = {}) -> None:
        print(f'开始下载数据')
        ds = DataSource()
        ds.qlib_data(name='qlib_data', target_dir='./work/datas/cn', region='cn', interval='1d')
        print(f'下载完成')

    @staticmethod
    def check_data(params:Dict = {}) -> None:
        print(f"Qlib Version: {qlib.__version__}")
        # 数据初始化
        provider_uri = "./work/datas/cn"
        qlib.init(
            provider_uri=provider_uri,
            region=REG_CN,
            joblib_backend="sequential",
            enable_multiprocessing=False
        )

        # --- 数据探索 ---
        # 1. 获取股票池
        # 你可以根据需求选择不同的股票池, 如 'csi300', 'csi500', 'all'
        # 'all' 代表全市场股票
        print("\n正在加载所以股票...")
        instruments = D.instruments(market='all')
        print(f"加载了 {len(instruments)} 只股票。")
        print(instruments)